fix: keep the referenced type when a $ref has complex siblings - #1832
Open
george-oakling wants to merge 1 commit into
Open
fix: keep the referenced type when a $ref has complex siblings#1832george-oakling wants to merge 1 commit into
george-oakling wants to merge 1 commit into
Conversation
A schema carrying a $ref next to `not`, `allOf`, `oneOf` or `anyOf` is routed to
the complex parser, which parses the complex keyword and drops the reference.
With `not` — the one complex keyword that cannot be expressed in TypeScript —
the property collapses to `any`:
constrainedRef:
$ref: '#/components/schemas/Status'
not: { enum: [STATUS_UNSPECIFIED] }
// before
constrainedRef?: any;
// after
constrainedRef?: Status;
Sibling keywords beside a $ref are legal in OpenAPI 3.1, and are how
protoc-gen-connect-openapi renders a protobuf enum field that carries a
protovalidate `not_in` rule, so a document generated from protobuf loses the
type on every such field: 156 properties in a 678-operation document.
The reference is now parsed alongside the complex content, with `any` members
filtered out of the intersection the same way AllOfSchemaParser already does. A
$ref with only annotation siblings (title, description) was already handled and
is unchanged.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A schema that carries a
$refnext tonot,allOf,oneOforanyOfis routed toComplexSchemaParser, which parses the complex keyword and drops the reference. Withnot— the one complex keyword that has no TypeScript equivalent, soNotSchemaParserreturnsany— the whole property collapses:Sibling keywords beside a
$refare legal in OpenAPI 3.1, where they compose with the referenced schema. They are also whatprotoc-gen-connect-openapiemits for a protobuf enum field carrying a protovalidatenot_inrule — the common "must not be the UNSPECIFIED member" constraint. In a 678-operation document generated that way, this turned 156 properties intoany; with this change the same document generates 11, all of them genuinely dynamic (the client's owncontentFormatters, a map value,google.protobuf.Any).The fix parses the reference alongside the complex content and filters
anyout of the intersection, reusing theignoreTypesidiomAllOfSchemaParseralready applies. A$refwith only annotation siblings (title,description) took the primitive path and already resolved correctly; that behaviour is unchanged, and the new fixture covers all three shapes.Tests:
tests/spec/ref-with-sibling-keywordsadded.vitest rungoes from 283 to 284 passing with no snapshot churn elsewhere; the 5 failures inpaths-2andpaths-2-prefer-existing-schema-namesfail identically on a clean checkout ofmain.Summary by cubic
Fixes
$refschemas with complex siblings likenot,allOf,oneOf, oranyOfso the referenced type is preserved instead of collapsing toany.protoc-gen-connect-openapiemits for protobuf enum fields withnot_inrules.anymembers are filtered out of the intersection.any.$refwith only annotation siblings already resolved correctly and is unchanged.not.Written for commit c3a86d4. Summary will update on new commits.